import { defineStore } from "pinia";
import { get, post } from "../utils/request.js";

export const useAttachmentStore = defineStore("attachment", {
    state: () => {
        return {
            currentFileInfo: { info: {}, index: null }, //当前选中的附件详情. 支撑附件详情组件,
            uploadConfig: null, // 上传配置信息
        };
    },
    getters: {
        // 格式化文件大小
        formattedMaxSize: (state) => {
            if (!state.uploadConfig?.uploadMaxSize) return "0 B";

            const bytes = state.uploadConfig.uploadMaxSize;
            const units = ["B", "KB", "MB", "GB"];
            let index = 0;
            let size = bytes;

            while (size >= 1024 && index < units.length - 1) {
                size /= 1024;
                index++;
            }

            return Math.round(size * 100) / 100 + " " + units[index];
        },

        // 获取所有允许的文件扩展名
        allowedExtensions: (state) => {
            if (!state.uploadConfig?.allowedFileTypes) return "";

            const allExtensions = [];
            Object.values(state.uploadConfig.allowedFileTypes).forEach((typeInfo) => {
                allExtensions.push(...typeInfo.ext);
            });

            return allExtensions.join("、");
        },
    },
    actions: {
        // 获取上传配置信息
        async getUploadConfig() {
            try {
                const response = await get("/sunAdmin/attachment/getUploadConfig");
                if (response.code !== 200) {
                    throw new Error(response.message);
                }
                this.uploadConfig = response.data;
                return response.data;
            } catch (error) {
                console.error("获取上传配置失败:", error);
                throw error;
            }
        },

        getList(where = {}, page = 1, limit = 20) {
            return post("/sunAdmin/attachment/getList", {
                where: where,
                page: page,
                limit: limit,
            });
        },

        del(ids = []) {
            return post("/sunAdmin/attachment/delete", {
                ids: ids,
            });
        },

        // 获取文件系统配置列表
        async getFilesystemConfigs() {
            try {
                const response = await get("/sunAdmin/attachment/getFilesystemConfigs");
                if (response.code !== 200) {
                    throw new Error(response.message);
                }
                return response.data;
            } catch (error) {
                console.error("获取文件系统配置失败:", error);
                throw error;
            }
        },

        // 更新文件系统配置
        async updateFilesystemConfig(configData) {
            try {
                const response = await post("/sunAdmin/attachment/updateFilesystemConfig", configData);
                if (response.code !== 200) {
                    throw new Error(response.message);
                }
                return response.data;
            } catch (error) {
                console.error("更新文件系统配置失败:", error);
                throw error;
            }
        },
    },
});
